package com.ve.view.ext;

import android.content.Context;
import android.view.ActionMode;
import android.view.Menu;
import android.view.MenuItem;

import com.ve.view.editor.Editor;

public class SelectActionLayout {
    protected Editor editor;
    private Context context;
    private ActionMode actionMode;
    private static final int icons[] = {
            android.support.v7.appcompat.R.drawable.abc_ic_menu_selectall_mtrl_alpha,
            android.support.v7.appcompat.R.drawable.abc_ic_menu_cut_mtrl_alpha,
            android.support.v7.appcompat.R.drawable.abc_ic_menu_copy_mtrl_am_alpha,
            android.support.v7.appcompat.R.drawable.abc_ic_menu_paste_mtrl_am_alpha,
    };
    private static final String[] labels = {"全选", "剪切", "复制", "黏贴"};

    public SelectActionLayout(Editor editor) {
        this.editor = editor;
        context = editor.getContext();


    }


    public void show() {
        if (actionMode == null)
            actionMode = editor.startActionMode(new ActionMode.Callback() {
                @Override
                public boolean onCreateActionMode(ActionMode mode, Menu menu) {
                    mode.setTitle("");
                    for (int i = 0; i < labels.length; i++) {
                        menu.add(0, i, 0, labels[i])
                                .setShowAsActionFlags(MenuItem.SHOW_AS_ACTION_ALWAYS)
                                .setIcon(icons[i]);
                    }
                    return true;
                }

                @Override
                public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
                    return false;
                }

                @Override
                public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
                    switch (item.getItemId()) {
                        case 0:
                            editor.getSelectInterface().selectAll();
                            break;
                        case 1:
                            editor.getTextInterface().cut();
                            mode.finish();
                            break;
                        case 2:
                            editor.getTextInterface().copy();
                            mode.finish();
                            break;
                        case 3:
                            editor.getTextInterface().paste();
                            mode.finish();
                    }
                    return false;
                }

                @Override
                public void onDestroyActionMode(ActionMode p1) {
                    editor.getSelectInterface().cancelSelect();
                    actionMode = null;
                }
            });

    }

    public void hide() {
        if (actionMode != null) {
            actionMode.finish();
            actionMode = null;
        }
    }

}
